<?php

namespace app\models;

use app\librarys\Tools;
use yii\mongodb\ActiveRecord;

class SqlLog extends ActiveRecord
{
    /**
     * 保存sql日志
     * @param $controllerId
     * @param $actionId
     * @param $md5_name
     * @param $type
     * @param $duration_time
     * @param $cmd
     * @param $is_cache
     * @return mixed|null|string
     */
    public static function add($controllerId, $actionId, $md5_name, $type, $duration_time, $cmd, $is_cache)
    {
        $log = new SqlLog();
        $log->_id = Tools::uuid();
        $log->controllerId = $controllerId;
        $log->actionId = $actionId;
        $log->md5_name = $md5_name;
        $log->type = $type;
        $log->cmd_rawSql = $cmd->rawSql;
        $log->createDatetime = time();
        $log->durationTime = $duration_time;
        $log->is_cache = $is_cache;
        $result = $log->save();
        if ($result == true) {
            return $log->_id;
        } else {
            return null;
        }
    }

    public static function getList($page, $pageSize, $controllerId, $actionId, $md5_name, $durationTime, $startTime, $endTime, $is_cache)
    {
        $whereParams = [];

        if (!empty($controllerId)) {
            $whereParams['controllerId'] = $controllerId;
        }

        if (!empty($actionId)) {
            $whereParams['actionId'] = $actionId;
        }

        if (!empty($md5_name)) {
            $whereParams['md5_name'] = $md5_name;
        }

        if ($is_cache != -1) {
            $whereParams['is_cache'] = (int)$is_cache;
        }

        $items = self::find()->where($whereParams);

        if (!empty($durationTime)) {
            if (!empty($whereParams)) {
                $items->andWhere(['>=', 'durationTime', intval($durationTime)]);
            } else {
                $items->where(['>=', 'durationTime', intval($durationTime)]);
            }
        }


        if (!empty($startTime)) {
            $stime = strtotime($startTime);
            if (!empty($whereParams) || !empty($durationTime)) {
                $items->andWhere(['>=', 'createDatetime', $stime]);
            } else {
                $items->Where(['>=', 'createDatetime', $stime]);
            }
        }

        if (!empty($endTime)) {
            $etime = strtotime($endTime);
            if (!empty($whereParams) || !empty($durationTime) || !empty($startTime)) {
                $items->andWhere(['<', 'createDatetime', $etime]);
            } else {
                $items->where(['<', 'createDatetime', $etime]);
            }
        }

        return $items->offset($page * $pageSize)
            ->limit($pageSize)
            ->orderBy('createDatetime desc')
            ->asArray()
            ->all();
    }

    public static function getCount($controllerId, $actionId, $md5_name, $durationTime, $startTime, $endTime, $is_cache)
    {
        $whereParams = [];

        if (!empty($controllerId)) {
            $whereParams['controllerId'] = $controllerId;
        }

        if (!empty($actionId)) {
            $whereParams['actionId'] = $actionId;
        }

        if (!empty($md5_name)) {
            $whereParams['md5_name'] = $md5_name;
        }

        if ($is_cache != -1) {
            $whereParams['is_cache'] = (int)$is_cache;
        }

        $items = self::find()->where($whereParams);

        if (!empty($durationTime)) {
            if (!empty($whereParams)) {
                $items->andWhere(['>=', 'durationTime', intval($durationTime)]);
            } else {
                $items->where(['>=', 'durationTime', intval($durationTime)]);
            }
        }


        if (!empty($startTime)) {
            $stime = strtotime($startTime);
            if (!empty($whereParams) || !empty($durationTime)) {
                $items->andWhere(['>=', 'createDatetime', $stime]);
            } else {
                $items->Where(['>=', 'createDatetime', $stime]);
            }
        }
        if (!empty($endTime)) {
            $etime = strtotime($endTime);
            if (!empty($whereParams) || !empty($durationTime) || !empty($startTime)) {
                $items->andWhere(['<', 'createDatetime', $etime]);
            } else {
                $items->where(['<', 'createDatetime', $etime]);
            }
        }
        return $items->count();
    }

    public function attributes()
    {
        return [
            '_id',  // pk 前台操作日志
            'controllerId',  // 请求的 controller id
            'actionId',  // 请求的 action id
            'md5_name',     //sql 别名
            'is_cache', //是否缓存
            'type', //类型,execute,query_all,query_one,query_scalar
            'cmd_rawSql',  //sql语句
            'createDatetime',  // 开始请求时间
            'durationTime'  // 请求持续时间(毫秒)
        ];
    }
}